package com.villaday.idf.fragment;


import android.content.Intent;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.activity.FindDetailsActivity;
import com.villaday.idf.adapter.HomeListFindAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseFragment;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.model.RecommendEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * 发现
 */
public class HomeListFindFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, AbsRecyclerViewAdapter.OnItemClickListener, View.OnClickListener, OnAdapterCallbackListener, OkHttpUtil.OnDataListener {


    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    Unbinder unbinder;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();

    private int pageNo = 1;
    private int nextIndex = 1;
    private int pageSize = 10;
    List<RecommendEntity.BodyEntity.DataListEntity> data = new ArrayList();
    private HomeListFindAdapter homeListFindAdapter;
    private ErrorLayout errorLayout;

    public static HomeListFindFragment newInstance() {
        HomeListFindFragment fragment = new HomeListFindFragment();
        return fragment;
    }


    @Override
    protected int getViewResId() {
        return R.layout.fragment_home_list_find;
    }

    @Override
    protected void init(View v) {
        loadingDialog = LoadingDialog.newInstance("加载中");
        loadingDialog.show(getActivity().getFragmentManager(), "loading");
        srl.setOnRefreshListener(this);

        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        homeListFindAdapter = new HomeListFindAdapter(getContext(), this, this);
        rv.setAdapter(homeListFindAdapter);

        rv.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(getContext(), 7), getResources().getColor(R.color.colorBg), PhoneUtil.dp2px(getContext(), 8)));
        homeListFindAdapter.setOnItemClickListener(this);
       /* View inflate = View.inflate(getContext(), R.layout.recycle_head, null);
        homeListSaleAdapter.addHeaderView(inflate);*/
        homeListFindAdapter.setEmptyView(R.layout.empty_tips);
        homeListFindAdapter.setEmptyTips(R.id.tv_emptyTips, "没有更多了");

    }

    @Override
    protected void loadData() {
        getData();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", pageNo + "");
        map.put("pageSize", pageSize + "");
        OkHttpUtil.getJSON(getContext(), Constant.URL.recommend, map, this);
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        nextIndex = 1;
        getData();
    }

    //rv item 点击事件
    @Override
    public void onItemClick(View v, int position) {
        Intent intent = new Intent(getContext(), FindDetailsActivity.class);
        intent.putExtra("topicId", data.get(position).getId());
        startActivity(intent);
    }

    @Override//rv 里面控件  喜欢和分享
    public void onClick(View v) {

    }

    @Override
    public void onCallback() {
        if (nextIndex == pageNo + 1) {
            pageNo++;
            getData();
        }
    }

    @Override
    public void onResponse(String url, String json) {
        if (Constant.URL.recommend.equals(url)) {
            if (errorLayout != null) {

                errorLayout.setSuccess();
            }
            LogeUtil.e("recommend", json);
            dismissLoading();

            RecommendEntity recommendEntity = gson.fromJson(json, RecommendEntity.class);
            removeLoadingItem();
            if (pageNo == 1) {
                data.clear();
            }

            if ("200".equals(recommendEntity.getCode())) {
                   /* if (pageNo == 1 && presellEntity.getBody().getRecomendList() != null && presellEntity.getBody().getRecomendList().getList() != null) {

                        dataHead.addAll(presellEntity.getBody().getRecomendList().getList());
                        homeListSaleHeadAdapter.setData(dataHead);
                    }*/
                data.addAll(recommendEntity.getBody().getDataList());
                if (recommendEntity.getBody().isHasNext()) {
                    data.add(new RecommendEntity.BodyEntity.DataListEntity(1));
                    nextIndex = pageNo + 1;

                } else {
                    addBaseLine();
                }

            } else {
                addBaseLine();
            }
            homeListFindAdapter.setData(data);
        }
    }

    @Override
    public void onFailure(String url, String error) {
        dismissLoading();
        if (Constant.URL.recommend.equals(url)) {
            if (errorLayout != null) {

                errorLayout.setError(2);
            }
            ToastUtil.initToast(getContext(),"网络错误");

        }
    }

    private void dismissLoading() {

        if (srl != null && srl.isRefreshing()) {
            srl.setRefreshing(false);
        }
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    private void removeLoadingItem() {
        if (data.size() > 0) {
            if (data.get(data.size() - 1).getMyType() == 1) {
                data.remove(data.size() - 1);
            }
        }
    }

    private void addBaseLine() {
        if (pageNo != 1) {
            data.add(new RecommendEntity.BodyEntity.DataListEntity(2));
        }
    }

    public void setErrorLayout(ErrorLayout errorLayout) {

        this.errorLayout = errorLayout;
    }
}
